/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "meta/TypeMeta.h"

#include "common/BigEndianSignedInteger.h"
#include "common/BigEndianSignedShort.h"
#include "common/U8Ascii.h"

#include "MixedData.h"

type_name mixed_data_name = { "mixeddata" };

void mixed_data_init( mixed_data * data, int32_t i, int16_t s, char * str )
{
	data->anInt = i;
	data->aShort = s;
	data->anAscii = str;
}

void * mixed_data_construct( type_list * objects, type_map * map )
{
	type_item * item = type_list_head( objects );
	int anInt = type_item_get_u32( item );
	
	item = type_item_next( item );
	int16_t aShort = type_item_get_s16( item );
	
	item = type_item_next( item );
	char * str = (char *) type_item_get_ptr( item );
	
	mixed_data * data = (mixed_data *) type_malloc( sizeof( mixed_data ) );
	mixed_data_init( data, anInt, aShort, str );
	return data;
}

int mixed_data_read( type_input_stream * in, type_element * element )
{
	return type_reader_auto( in, element, &mixed_data_construct );
}

int mixed_data_write( type_output_stream * out, void * o, type_element * element )
{
	int rc;
	mixed_data * data = (mixed_data *) o;
	
	rc = type_output_write_named_object( (out), &sint32_name, &data->anInt );
	if ( rc != TYPE_OK ) return rc;
	
	rc = type_output_write_named_object( out, &sint16_name, &data->aShort );
	if ( rc != TYPE_OK ) return rc;
	
	rc = type_output_write_named_object( out, &u8ascii_name, data->anAscii );
	if ( rc != TYPE_OK ) return rc;
	
	return TYPE_OK;	
}

/* This is normally handled by a dictionary file.
 * For testing purposes we have created the entry manually
 */
int mixed_data_register( type_library * library )
{
    meta_sequence * dataSeq = (meta_sequence *) type_malloc( sizeof( meta_sequence ) );
    meta_reference * dataRefInt = (meta_reference *) type_malloc( sizeof( meta_reference ) );
    meta_reference * dataRefShort = (meta_reference *) type_malloc( sizeof( meta_reference ) );
    meta_reference * dataRefAscii = (meta_reference *) type_malloc( sizeof( meta_reference ) );
    meta_expression ** dataSeqObjs = (meta_expression **) type_malloc( sizeof( meta_expression *) * 3 );
    
    meta_reference_init( dataRefInt, type_library_get_id( library, &sint32_name ), "s32" );
    meta_reference_init( dataRefShort, type_library_get_id( library, &sint16_name ), "s16" );
    meta_reference_init( dataRefAscii, type_library_get_id( library, &u8ascii_name ), "u8ascii" );
    dataSeqObjs[0] = (meta_expression *) dataRefInt;
    dataSeqObjs[1] = (meta_expression *) dataRefShort;
    dataSeqObjs[2] = (meta_expression *) dataRefAscii;
    meta_sequence_init( dataSeq, dataSeqObjs, 3 );
	
	return type_library_register_bind( library, &mixed_data_name, (type_element *) dataSeq, &mixed_data_read, &mixed_data_write );
}
